//
//  CLStatusTableViewCell.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/13.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit
import SnapKit
/*
每条微博 都划分为三个板块
1.自己的原创微博
2.转发别人的微博
3.转发、评论、赞(自定义toolbar)
*/

//首页cell子控件的间距
let HomeCellMargin :CGFloat = 10
//微博首页cell
class CLStatusTableViewCell: UITableViewCell {

    //记录toolbar顶部约束
    var toolbarTopConstraint:Constraint?
    //使用ViewModel模型
    var statusViewModel:CLStatusViewModel?{
        didSet {
            //给原创微博视图设置需要的模型属性
            originalView.statusViewModel = statusViewModel
            //卸载约束
            toolbarTopConstraint?.uninstall()
            //判断是否有转发微博
            if statusViewModel?.status?.reweeted_status != nil {
                reweetView.statusViewModel = statusViewModel
                reweetView.hidden = false
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(reweetView.snp_bottom).constraint
                })
                
            }else {
                reweetView.hidden = true
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            toolBar.statusViewModel = statusViewModel
        }
    }
    
    // MARK:  懒加载空间
    //原创微博视图
    private lazy var originalView:CLOriginalView = CLOriginalView()
    //转发微博视图
    private lazy var reweetView:CLReweetView = CLReweetView()
    //toolbar视图
    private lazy var toolBar:CLToolBarView = CLToolBarView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加视图约束
    private func setUpUI(){
        
       //首页添加了视图 才能布置约束
        contentView.addSubview(originalView)
        contentView.addSubview(reweetView)
        contentView.addSubview(toolBar)
        //设置约束
        //原创视图的约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        //转发微博的是不
        reweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        //评论工具栏的约束
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolbarTopConstraint = make.top.equalTo(reweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        //确定cell的高度 contentView 的底部等于toolbar的底部
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
